<template>
    <div class="edit-popup">
        <popup
            ref="popupRef"
            :async="true"
            :title="popupTitle"
            width="550px"
            @close="handleClose"
            @confirm="handleSubmit"
        >
            <el-form ref="formRef" :model="formData" :rules="formRules" label-width="90px">
                <el-form-item label="名字" prop="name">
                    <el-input v-model="formData.name" clearable placeholder="请输入名字" />
                </el-form-item>
                <el-form-item label="手机号" prop="mobile">
                    <el-input v-model="formData.mobile" clearable placeholder="请输入手机号" />
                </el-form-item>
                <!--                <el-form-item label="账户ID" prop="account_id">-->
                <!--                    <el-input v-model="formData.account_id" clearable placeholder="请输入账户ID" />-->
                <!--                </el-form-item>-->
                <!--                <el-form-item label="余额" prop="money">-->
                <!--                    <el-input v-model="formData.money" clearable placeholder="请输入余额" />-->
                <!--                </el-form-item>-->
                <!--                <el-form-item label="冻结余额" prop="frozen_money">-->
                <!--                    <el-input v-model="formData.frozen_money" clearable placeholder="请输入冻结余额" />-->
                <!--                </el-form-item>-->
            </el-form>
        </popup>
    </div>
</template>

<script lang="ts" name="boomDealerEdit" setup>
import { ElMessage, type FormInstance } from 'element-plus'
import Popup from '@/components/popup/index.vue'
import { apiBoomDealerAdd, apiBoomDealerDetail, apiBoomDealerEdit } from '@/api/dealer'
import type { PropType } from 'vue'

defineProps({
    dictData: {
        type: Object as PropType<Record<string, any[]>>,
        default: () => ({})
    }
})
const emit = defineEmits(['success', 'close'])
const formRef = shallowRef<FormInstance>()
const popupRef = shallowRef<InstanceType<typeof Popup>>()
const mode = ref('add')


// 弹窗标题
const popupTitle = computed(() => {
    return mode.value == 'edit' ? '编辑经销商表' : '新增经销商表'
})

// 表单数据
const formData = reactive({
    id: '',
    name: '',
    mobile: '',
    account_id: '',
    money: '',
    frozen_money: ''
})


// 表单验证
const formRules = reactive<any>({
    name: [{
        required: true,
        message: '请输入名字',
        trigger: ['blur']
    }],
    mobile: [{
        required: true,
        message: '请输入手机号',
        trigger: ['blur']
    }],
    money: [{
        required: true,
        message: '请输入余额',
        trigger: ['blur']
    }]
})


// 获取详情
const setFormData = async (data: Record<any, any>) => {
    for (const key in formData) {
        if (data[key] != null && data[key] != undefined) {
            //@ts-ignore
            formData[key] = data[key]
        }
    }


}

const getDetail = async (row: Record<string, any>) => {
    const data = await apiBoomDealerDetail({
        id: row.id
    })
    setFormData(data)
}


// 提交按钮
const handleSubmit = async () => {
    await formRef.value?.validate()
    const data = { ...formData }
    //校验手机号码
    if (!/^1[3-9]\d{9}$/.test(data.mobile)) {
        ElMessage.error('手机号码格式不正确')
        return
    }
    mode.value == 'edit'
        ? await apiBoomDealerEdit(data)
        : await apiBoomDealerAdd(data)
    popupRef.value?.close()
    emit('success')
}

//打开弹窗
const open = (type = 'add') => {
    mode.value = type
    popupRef.value?.open()
}

// 关闭回调
const handleClose = () => {
    emit('close')
}


defineExpose({
    open,
    setFormData,
    getDetail
})
</script>
